//
// Created by xili on 2024/8/25 17:15.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int numSubarraysWithSum(vector<int> &nums, int goal) {
        int n = nums.size();
        unordered_map<int, int> ump;
        ump[0] = 1;
        int sum = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            sum += nums[i];
            ans += ump[sum - goal];
            ump[sum]++;
        }
        return ans;
    }
};